import pickle
possibleExits = ["North","South","West","East"]

class Room:
    def __init__(self):
        self.exits = []
        self.items = []
        self.description = ""
        self.roomNumber = ""
        self.roomName = ""
class RoomExit:
    def __init__(self):
        self.description = ""
        self.direction = ""
        self.targetRoomNumber = ""
class Items:
    def __init__(self):
        self.name = ""
        self.description = ""
        self.itemClassification = ""
        self.value = ""
def ListRooms(rooms):
    print "Room List:"
    print "---------------------------"
    for Room in rooms:
        print Room.roomName, "- Description:", Room.description , "- number:", Room.roomNumber, "- exits:", possibleExits   
def Menu():
    print "make a decision out of my menu:"
    print '      [0] List rooms.'
    print '      [1] Create room.'
    print '      [2] delete room'
    print '      [3] edit room'
    print '      [4] Leave!'
    
    return raw_input()

def createNewRoom():
    global possibleExits
    
    room = Room()
    
    print "CREATE NEW ROOM:"
    print "======================================="       
    print "What is the room number?",
    room.roomNumber = int(raw_input())
    
    print "What is the room name?",
    room.roomName = str(raw_input())
    
    print "What is the description?",
    room.description = str(raw_input())
    
    ##########
    addingExit = True
    while addingExit :
        roomExit = RoomExit()
        print ""
        print "CREATE NEW EXIT:"
        print "======================================="   
        print "What is the exit description?",
        roomExit.description = str(raw_input())    
        choosingDirection = True
        while choosingDirection: 
            print "pick one of the exit directions?([0]North,[1]South,[2]West,[3]East)",
            directionChoice = int(raw_input())
            if directionChoice >= 0 and directionChoice < 4:
                roomExit.direction = possibleExits[ directionChoice ] 
                choosingDirection = False
            else:
                print "That is not a valid direction!!!"
            
        print "What is the room number does it go to?",
        roomExit.targetRoomNumber = str(raw_input())
        
        room.exits.append(roomExit)

     
        print "would you like to add again?(yes,no)",
        answer = str(raw_input()) 
        if answer != "yes":
            addingExit = False
    #########
    
    return room           
def EditRooms(rooms):
    run = True
    numberOfRooms = len(rooms)
    while run :
        print "Choose a address to edit:"
        print "---------------------------"
        for Room in rooms:
            currentIndex = rooms.index(Room)+1
            print ("["+str(currentIndex)+"]"), Room.roomName
        choice = raw_input()
        if choice == "":
            run = False
        else:           
            choice = int(choice)
            if (choice >= 1 and choice <= numberOfRooms):
                room = rooms[choice-1]
                print "Editing "+ room.roomName+ "..."
                print "What is the room name?(press enter to keep \""+room.roomName+"\")",
                newName = raw_input()    
                if newName != "":
                    room.roomName = newName 
                print "What is the room number?(press enter to keep \""+str(room.roomNumber)+"\")",
                newNumber = raw_input()    
                if newNumber != "":
                    room.roomNumber = newNumber 
                print "What is the description?(press enter to keep \""+str(room.description)+"\")",
                newDescription = raw_input()    
                if newDescription != "":
                    room.description = newDescription
                    run = False
def SaveRooms(roomList):
    try:
        pickleFile = open("rooms.pkl", "w")
        pickle.dump(rooms, pickleFile)
        print "Saved rooms successfully!"
    except IOError as e:
        print "Failed to save rooms!"
                    
    return roomList
def LoadRooms():
    roomList = []
    try:
        pickleFile = open("rooms.pkl", "r")
        roomList = pickle.load(pickleFile)
        print "load successful!"
    except IOError as e:
        roomList = []
        print "load FAILED"
    return roomList
def DeleteRooms(rooms):
    run = True
    numberOfRooms = len(rooms)
    while run :
        print "Choose a room to delete:"
        print "---------------------------"
        for Room in rooms:
            currentIndex = rooms.index(Room)+1
            print ("["+str(currentIndex)+"]"), Room.roomName
        choice = int(raw_input())
        if (choice >= 1 and choice <= numberOfRooms):
            roomFromList = rooms[choice-1]
            print "Deleting", roomFromList.roomName, "...",
            del rooms[choice-1]
            print "DONE!"
            run = False
        else:
            print "Invalid Choice!"
rooms = LoadRooms()
running = True 
while running:
    choice = Menu()
    if choice == "4":
        running = False
        print "____________________good bye________________________________"
    elif choice == "1":
        rooms.append( createNewRoom() )
        SaveRooms(rooms)
    elif choice == "0":
        ListRooms(rooms)
        rooms
    elif choice == "2":
        DeleteRooms(rooms)
        SaveRooms(rooms)
    elif choice == "3":
        EditRooms(rooms)
        SaveRooms(rooms)
